<?php 
/*
   Copyright 2010, e-TAT

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   
   This is part of the e-TAT Architecture Package and will be featured in the final e-TAT Application (Phase 1).
*/
class Response{

	public function getLatestResponse($id) {
		$sql_get_response_id = "SELECT MAX(`id`)FROM `response` WHERE `student_id` = '$id'";
		$rid = mysql_query($sql_get_response_id)or die(mysql_error());
		$result_get_response_id = mysql_fetch_row($rid);
		foreach($result_get_response_id as $key=>$value) {
			$response_id = $value;
		}
		if($response_id) {
			return $response_id;
		} else {
			return false;
		}
	}
	public function saveResponse($data) {
		$sql = "INSERT INTO `response` (`survey_id`,`student_id`,`student_name`,`timestamp`)
	       VALUES ( '{$data['survey_id']}','{$data['student_id']}','{$data['student_name']}','{$data['timestamp']}')";
		$result = mysql_query($sql)or die ("Save Response: ". mysql_error());
		if($result) {
			return true;
		} else {
			return false;
		}
	}
	public function saveResponseValues($data) {
		$sql = "INSERT INTO `response_value` (`response_id`,`survey_id`,`question_id`,`response`)
	       VALUES ('{$data['response_id']}','{$data['survey_id']}','{$data['question_id']}','{$data['response']}')";
		$result = mysql_query($sql)or die("Save Response Value: ".mysql_error());
		if($result) {
			return true;
		} else {
			return false;
		}

	}
	public function updateResponseValues($data){
		$sql = "UPDATE `response_value` SET `response`='{$data['response']}'
			WHERE `question_id`='{$data['question_id']}' AND `response_id`='{$data['response_id']}'";
		$result = mysql_query($sql)or die("Save Response Value: ".mysql_error());
		if($result) {
			return true;
		} else {
			return false;
		}
	}
	public function checkResponse($response_id, $question_id) {
		$sql = "SELECT * FROM `response_value` WHERE `question_id`='$question_id' AND `response_id`='$response_id'";
		$result = mysql_query($sql)or die("Check Question Response: ".mysql_error());
		$response = mysql_fetch_row($result);
		if($response) {
			return true;
		} else {
			return false;
		}
	}
	public function getResponseValue($response_id,$question_id) {
		$sql = "SELECT `response` FROM `response_value` WHERE `question_id`='$question_id' AND `response_id`='$response_id'";
		$result = mysql_query($sql)or die("GET Question Response: ".mysql_error());
		$response = mysql_fetch_object($result);
		if($response) {
			return $response->response;
		} else {
			return false;
		}
	}
}
?>